package com.multi.thread.chapter06.demo02;

/**
 *	延迟加载
 */
public class Singleton {
	
	private Singleton() {};
	
	private static Singleton singleton;
	
	public static Singleton getInstance() {
		if (singleton == null) {
			synchronized (Singleton.class) {
				if (singleton == null) {
					singleton = new Singleton();
				}
			}
		}
		return singleton;
	}
	
	static private class TestThread extends Thread {
		@Override
		public void run() {
			System.out.println("threadName : " + Thread.currentThread().getName() + ", hashcode : " 
					+ Singleton.getInstance().hashCode());
		}
	}
	
	public static void main(String[] args) {
		TestThread t1 = new TestThread();
		TestThread t2 = new TestThread();
		TestThread t3 = new TestThread();
		t1.start();
		t2.start();
		t3.start();
	}

}
